﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace DCT.View
{
	public class DraggedAdorner : Adorner
	{
		private ContentPresenter contentPresenter;
		private double left;
		private double top;
		private AdornerLayer adornerLayer;

		public DraggedAdorner(Drawing dragDropDrawing, DataTemplate dragDropTemplate, UIElement adornedElement, AdornerLayer adornerLayer)
			: base(adornedElement)
		{
			this.adornerLayer = adornerLayer;
            
			this.contentPresenter = new ContentPresenter();
            this.contentPresenter.Content = dragDropDrawing;
			this.contentPresenter.ContentTemplate = dragDropTemplate;
			this.contentPresenter.Opacity = 0.7;
            //为框形设备的线宽留余量
            this.contentPresenter.Width = adornedElement.DesiredSize.Width + DragDropHelper.HorFix;
            this.contentPresenter.Height = adornedElement.DesiredSize.Height + DragDropHelper.VerFix;

			this.adornerLayer.Add(this);
		}

		public void SetPosition(double left, double top)
		{
			this.left = left;
			this.top = top;
			if (this.adornerLayer != null)
			{
				this.adornerLayer.Update(this.AdornedElement);
			}
		}

        protected override Size MeasureOverride(Size constraint)
        {
            this.contentPresenter.Measure(constraint);
            return this.contentPresenter.DesiredSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            this.contentPresenter.Arrange(new Rect(finalSize));
            return finalSize;
        }

		protected override Visual GetVisualChild(int index)
		{
			return this.contentPresenter;
		}

		protected override int VisualChildrenCount
		{
			get { return 1; }
		}

		public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
		{
			GeneralTransformGroup result = new GeneralTransformGroup();
			result.Children.Add(base.GetDesiredTransform(transform));
			result.Children.Add(new TranslateTransform(this.left, this.top));

			return result;
		}

		public void Detach()
		{
			this.adornerLayer.Remove(this);
		}

	}
}
